package com.autonavi.yunda.yunji.core.configuration;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.taobao.tddl.client.jdbc.TDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author cizhong.wcs
 * @date 2022/3/15 5:26 下午
 */
@Slf4j
@Configuration
public class DataSourceConfiguration {
    @Value("${tddl.appName}")
    String appName;

    @Bean
    @Primary
    public DataSource dataSource() {
        TDataSource tdDataSource = new TDataSource();
        AmapPreconditions.checkNotNull(appName);
        tdDataSource.setAppName(appName);
        tdDataSource.setDynamicRule(true);
        tdDataSource.setSharding(false);
        //pouch化需要setUnitName指向center
        tdDataSource.setUnitName("center");
        tdDataSource.init();
        return tdDataSource;
    }
}
